package com.ruoyi.ai.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.ai.domain.vo.WechatLoginVO;
import com.ruoyi.ai.service.WechatLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 微信登录控制器
 */
@RestController
@RequestMapping("/wechat")
public class WechatLoginController {

    @Autowired
    private WechatLoginService wechatLoginService;

    /**
     * 微信小程序登录接口
     */
    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody WechatLoginVO loginVO) {
        // 参数校验
        if (StringUtils.isEmpty(loginVO.getCode())) {
            return R.fail("登录凭证不能为空");
        }

        try {
            // 调用服务处理微信登录
            Map<String, Object> result = wechatLoginService.wechatLogin(loginVO);
            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("登录失败：" + e.getMessage());
        }
    }
}